home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / modlib_s.lha / modlib_src / $consult.P < prev    next >
Text File  |  1990-04-12  |  5KB  |  127 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* $consult.P */
  25.  
  26. /* consult(Filename) consults the named file for interpretation.
  27.    The second (optional) parameter to consult is a list of options.
  28.    A `v' means verbose. An `e' means expand macros.  `t' means put trace
  29.    points on any untraced predicate in the file being consulted. `q'
  30.    means do a "quick" consult.
  31.     (Macros are clauses with ::-. They are evaluated, if possible,
  32.     at compile time. They MUST be PURE Horn clauses)
  33.    The third (optional) parameter is used to return a list of
  34.    predicate/arity pairs that were defined by the consult. This list
  35.    can be passed to trace/1 so that they can be traced.
  36. */
  37.  
  38. $consult_export([$consult/1,$consult/2,$consult/3,$consult_list/1]).
  39.  
  40. /* $consult_use($blist,[$append/3,$member/2,$memberchk/2]).
  41.    $consult_use($bio,[$writename/1,$writeqname/1,$put/1,$nl/0,$tab/1,
  42.     $tell/1,$tell/2,$telling/1,$told/0,$get/1,$get0/1,$see/1,$seeing/1,
  43.     $seen/0]).
  44.    $consult_use($assert,[$assert/1,$asserti/2,$assert_union/2,$assert_call_s/1,
  45.         $assert_get_prref/2,$assert_put_prref/2,$assert_abolish_i/1]).
  46.    $consult_use($getclauses,[$getclauses/2,$getclauses/3,_]).
  47.    $consult_use($bmeta,[$atom/1,$atomic/1,$integer/1,$number/1,$structure/1,
  48.     $functor0/2,$bldstr/3,$arg/3,$arity/2,$real/1,$floor/2]).
  49.    $consult_use($mac,[$macexp/3]).
  50. */
  51.  
  52. $consult(File) :- $consult(File,[e]).
  53.  
  54. $consult(File,Opts) :- not(not($consult(File,Opts,_))). /* side-effects only*/
  55.  
  56. $consult(File,Opts,Predlist) :-
  57.     $atomic(File) ->
  58.          ($consultable(File) ->
  59.               ($getclauses(File,Clslist,Predlist),!,
  60.               $macexp(Clslist,Opts,Nclslist),
  61.               $consult_s(File,Nclslist,Opts),
  62.               $symtype('_$traced_preds'(_),Tr),
  63.               (Tr > 0 -> $consult_untrace(Predlist) ; true),
  64.               ($memberchk(t,Opts) -> $consult_trace(Predlist) ; true),
  65.                !
  66.              ) ;
  67.         ($print('** No such file: '), $print(File), $nl)
  68.          ) ;
  69.          ($print('** Illegal filename '), $print(File),
  70.           $print(' for consult: filename must be atomic'), $nl
  71.          ).
  72.  
  73. $consultable(user).
  74. $consultable(File) :- $exists(File).
  75.  
  76. $consult_s(File,Clslist,Opts) :-
  77. /*  Clslist is list of terms: pred(Pred,Arity,_,_,ClauseList)
  78.     where Clauselist is a list of terms:
  79.     fact(Fact,CpyFlagRest) or rule(Head,Body,CpyFlag,CpyFlagRest) */
  80.  
  81.     $member(pred(Pred,Arity,_,_,Clauselist),Clslist),
  82.     $bldstr(Pred,Arity,Call),$consult_abolish(Call),    
  83.     ($member(v,Opts)->
  84.       $tab(15),$writename('including : '),$writename(Pred),
  85.       $writename('/'),$writename(Arity),$nl
  86.     ;
  87.       true),
  88.     $consult_islist(Clauselist),
  89.     $member(Factorrule,Clauselist),
  90.     $consult_assert(Factorrule),
  91.     fail.
  92. $consult_s(_,_,_).
  93.  
  94. $consult_islist([]) :- !.
  95. $consult_islist([_|Tail]) :- $consult_islist(Tail).
  96.  
  97. $consult_abolish(Goal) :- $buff_code(Goal,0,11,0).
  98.  
  99. $consult_assert(fact(Fact,_)) :- $assert(Fact).
  100. $consult_assert(rule(Head,Body,_,_)) :-    $assert((Head:-Body)).
  101.  
  102. $consult_untrace([]).
  103. $consult_untrace(['/'(P,N)|Prest]) :-
  104.     ('_$traced_preds'(P/N) ->
  105.         ($retract('_$traced_preds'(P/N)), trace(P/N)) ;
  106.         true
  107.     ),
  108.     $consult_untrace(Prest).
  109.  
  110. $consult_trace(L) :-
  111.     $symtype('_$traced_preds'(_),N),
  112.     (N > 0 -> $consult_trace1(L) ; trace(L)).
  113.  
  114. $consult_trace1([]).
  115. $consult_trace1([P|Prest]) :-
  116.     ('_$traced_preds'(P) -> true ; trace(P)),
  117.     $consult_trace1(Prest).
  118.  
  119. $consult_list([]).
  120. $consult_list([H|T]) :- $consult_list1(H), $consult_list(T).
  121.  
  122. $consult_list1('-'(File)) :- !, $consult(File).
  123. $consult_list1(File) :- $consult(File).
  124.  
  125. /* ------------------------------ $consult.P ------------------------------ */
  126.  
  127.